import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from 'axios'

export const useUserStore = defineStore('user', () => {
  const username = ref('')
  const token = ref(localStorage.getItem('token') || '')
  
  // 模拟登录接口
  const login = async (userInfo) => {
    try {
      // 实际项目中应该用真实接口替换
      // const res = await axios.post('/api/login', userInfo)
      // 这里模拟成功登录
      const mockResponse = {
        data: {
          token: 'mock-token-123456',
          username: userInfo.username
        }
      }
      
      token.value = mockResponse.data.token
      username.value = mockResponse.data.username
      localStorage.setItem('token', token.value)
      
      return Promise.resolve(mockResponse)
    } catch (error) {
      return Promise.reject(error)
    }
  }
  
  // 退出登录
  const logout = () => {
    username.value = ''
    token.value = ''
    localStorage.removeItem('token')
  }
  
  return {
    username,
    token,
    login,
    logout
  }
}) 